/****

 activequant - activestocks.eu

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 
 contact  : contact@activestocks.eu
 homepage : http://www.activestocks.eu

 ****/
package org.activequant.data.retrieval.ib;

import java.util.Calendar;

import org.activequant.core.domainmodel.SeriesSpecification;
import org.activequant.core.domainmodel.data.CandleSeries;
import org.activequant.core.types.TimeStamp;
import org.activequant.data.annotations.Vendor;
import org.activequant.data.retrieval.ICandleSeriesSource;
import org.activequant.util.ib.IBTwsConnection;
import org.apache.log4j.Logger;

/**
 * 
 * abstracted Interactive Broker Candle series source, requires a properly configured
 * monitor. <br>
 * IBCandleSeriesSource implements ICandleSeriesSource. Holds the following associated variable:
 * <ul>
 * <li>monitor(IBTwsConnection)</li>
 * </ul>
 * <b>History:</b><br>
 *  - [24.06.2007] Created (Ulrich Staudinger)<br>
 *  - [11.08.2007] Added array functions (Erik Nijkamp)<br>
 *  - [14.08.2007] removing symbol code, extending from CandleSeriesSourceBase. (Ulrich Staudinger)<br>
 *  - [20.01.2008] Removed getVendor(), added annotation, removed main() (Erik Nijkamp)<br>
 *  - [05.05.2008] eXtracting constants for data types (Ulrich Staudinger)<br>
 *
 *  @author Ulrich Staudinger
 *  @author Erik Nijkamp
 */
@Vendor("IB")
public class IBCandleSeriesSource implements ICandleSeriesSource {
	/**
	 * private IBTwsConnection monitor;
	 */
	private IBTwsConnection monitor;

	protected final static Logger log = Logger.getLogger(IBCandleSeriesSource.class);
	/**
	 * constructs an IBCandleSeriesSource(implements ICandleSeriesSource) using the given monitor(IBTwsConnection) to set its associated monitor(IBTwsConnection)
	 * @param monitor
	 */
	public IBCandleSeriesSource(IBTwsConnection monitor) {
		this.monitor = monitor;
	}

	/**
	 * returns a CandleSeries for the given query(SeriesSpecification) (by fetching historical data from IB)<br/>
	 * to request BID or ASK instead of the default TRADES data points, 
	 * add a field this.DATA_TYPE into the decoration of SeriesSpecification.getInstrumentSpec().<br>
	 * <br>
	 * Example: <br> 
	 * query.getInstrumentSpecification().addDecoration(IBTwsConnection.DATA_TYPE, IBTwsConnection.BIDASK);
	 */
	public CandleSeries fetch(SeriesSpecification query) throws Exception {
		
		if(query.getTimeFrame() == null) {
			throw new NullPointerException("timeFrame");
		}

		TimeStamp startDate = query.getStartTimeStamp();
		TimeStamp endDate = query.getEndTimeStamp();
		
		if(endDate == null) {
			log.warn("end date not specified: assuming now");
			endDate = new TimeStamp();
		}
		
		if(startDate == null) {
			log.warn("start date not specified: assuming 1 day before end");
			Calendar cal = Calendar.getInstance();
			cal.setTime(endDate.getDate());
			cal.add(Calendar.DAY_OF_YEAR, -1);
			startDate = new TimeStamp(cal.getTime());
		}
		
		String type = IBTwsConnection.TRADES;
		if(query.getInstrumentSpecification().containsDecoration(IBTwsConnection.DATA_TYPE)){
			type = query.getInstrumentSpecification().getDecoration(IBTwsConnection.DATA_TYPE);
		}
		
		String barSize = IBTwsConnection.timeFrameToBarSize(query.getTimeFrame());
		return monitor.fetch(startDate, endDate, barSize, type, query, true);
	}
}
